package io.kiki.sba.registry.store.api;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
public class DBResponse<T> implements Serializable {

    private final T entity;

    private final OperationStatus operationStatus;


    public DBResponse(T entity, OperationStatus operationStatus) {
        this.entity = entity;
        this.operationStatus = operationStatus;
    }


    public static DBResponseBuilder ok() {
        return setStatus(OperationStatus.success);
    }


    public static <T> DBResponseBuilder ok(T entity) {
        DBResponseBuilder b = ok();
        b.entity(entity);
        return b;
    }


    public static DBResponseBuilder notfound() {
        return setStatus(OperationStatus.notfound);
    }


    protected static DBResponseBuilder setStatus(OperationStatus status) {
        DBResponseBuilder b = DBResponseBuilder.getInstance();
        b.status(status);
        return b;
    }


    public static class DBResponseBuilder<T> {
        private T entity;
        private OperationStatus operationStatus;

        private DBResponseBuilder() {
        }


        public static DBResponseBuilder getInstance() {
            return new DBResponseBuilder();
        }


        public DBResponse build() {
            return new DBResponse(entity, operationStatus);
        }


        public DBResponseBuilder status(@NonNull OperationStatus operationStatus) {

            this.operationStatus = operationStatus;
            return this;
        }


        public DBResponseBuilder entity(T entity) {
            this.entity = entity;
            return this;
        }
    }
}
